package com.yanqu.road.logic.pb;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.activity.game.config.StrengthConfig;
import com.yanqu.road.entity.activity.yearMaster.GridDetail;
import com.yanqu.road.entity.activity.yearMaster.UserYearMasterData;
import com.yanqu.road.entity.activity.yearMaster.YearMasterBoss;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.pb.yearMaster.YearMasterProto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class YearMasterPb {


    public static YearMasterProto.YearMasterRespBaseDataEntity parseYearMasterBaseRespMsg(UserYearMasterData userYearMasterData, YearMasterProto.YearMasterBossInfo bossInfo,long highScore) {
        YearMasterProto.YearMasterRespBaseDataEntity.Builder builder = YearMasterProto.YearMasterRespBaseDataEntity.newBuilder();
        builder.setStrength(userYearMasterData.getPhysicalStrength());
        if (bossInfo != null) {
            builder.setBossInfo(bossInfo);
        }
        List<GridDetail> gridDetails = JSONObject.parseArray(userYearMasterData.getPaint(), GridDetail.class);
        List<GridDetail> highGridDetails = JSONObject.parseArray(userYearMasterData.getHighPaint(), GridDetail.class);
        List<GridDetail> fastGridDetails = JSONObject.parseArray(userYearMasterData.getFastPaint(), GridDetail.class);
        builder.addAllPaint(parseGridMsgList(gridDetails));
        builder.addAllHighPaint(parseGridMsgList(highGridDetails));
        builder.addAllFastPaint(parseGridMsgList(fastGridDetails));
        builder.setHighGridOpen(userYearMasterData.getScore()>=highScore?1:0);
        return builder.build();
    }

    public static YearMasterProto.YearMasterRespBaseDataEntity parseYearMasterBaseRespMsg(YearMasterBoss bossInfo, UserYearMasterData userYearMasterData,long highScore) {
        if (bossInfo != null) {
           return parseYearMasterBaseRespMsg(userYearMasterData, parseBossInfo(bossInfo).build(),highScore);
        }
        return parseYearMasterBaseRespMsg(userYearMasterData, null,highScore);
    }

    public static YearMasterProto.YearMasterBossInfo.Builder parseBossInfo(YearMasterBoss bossInfo) {
        YearMasterProto.YearMasterBossInfo.Builder builder = YearMasterProto.YearMasterBossInfo.newBuilder();
        builder.setActivityId(bossInfo.getActivityId());
        builder.setBossLevel(bossInfo.getBossLevel());
        builder.setBossState(bossInfo.getState());
        builder.setCurrentBlood(bossInfo.getCurrentBlood());
        builder.setTotalBlood(bossInfo.getTotalBlood());
        builder.setBossLastLvBlood(bossInfo.getLastLvBlood());
        return builder;
    }

    public static List<YearMasterProto.GridPaintMsg> parseGridMsgList(List<GridDetail> gridDetails) {
        List<YearMasterProto.GridPaintMsg> list = new ArrayList<>();
        for (GridDetail gridDetail : gridDetails) {
            YearMasterProto.GridPaintMsg.Builder builder = YearMasterProto.GridPaintMsg.newBuilder();
            builder.setGridId(gridDetail.getGridType());
            list.add(builder.build());
        }
        return list;
    }

    public static YearMasterProto.YearMasterActivityDetailMsg.Builder parseActivityInfo(StrengthConfig strengthConfig, UserYearMasterData userYearMasterData, YearMasterBoss yearMasterDetail, Map<Integer,Long> stateHpMap,long highOpenScore) {
        YearMasterProto.YearMasterActivityDetailMsg.Builder builder = YearMasterProto.YearMasterActivityDetailMsg.newBuilder();
        builder.setActivityId(userYearMasterData.getActivityId());
        builder.setMaxStrength(strengthConfig.getMaxStrength());
        builder.setRestoreStrength(strengthConfig.getAddStrength());
        builder.setLastStrengthTime(userYearMasterData.getStrengthTime()/1000);
        builder.setIntervalTime(strengthConfig.getSecends());
        builder.setBaseInfo(parseYearMasterBaseRespMsg(yearMasterDetail, userYearMasterData,highOpenScore));
        //1是活动积分 2 恢复体力道具 3 送财烟火
        builder.addProps(YearMasterProto.YearMasterPropsMsg.newBuilder().setType(1).setId(eItemId.YEAR_MASTER_SCORE.getValue()).build());
        builder.addProps(YearMasterProto.YearMasterPropsMsg.newBuilder().setType(2).setId(eItemId.YEAR_MASTER_EGG.getValue()).build());
        builder.addProps(YearMasterProto.YearMasterPropsMsg.newBuilder().setType(3).setId(eItemId.YEAR_MASTER_FIREWORKS.getValue()).build());

        builder.setHighOpenScore(highOpenScore);
        for(Map.Entry<Integer,Long> entry:stateHpMap.entrySet()){
            YearMasterProto.YearMasterStateHp.Builder stateBuilder = YearMasterProto.YearMasterStateHp.newBuilder();
            stateBuilder.setState(entry.getKey());
            stateBuilder.setHp(entry.getValue());
            builder.addStateHps(stateBuilder);
        }
        return builder;
    }
}
